#pragma GCC optimize(2)
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#include <stack>
#include <bitset>
#define F first
#define S second
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;

void solve()
{
	string s;
	cin>>s;
	int l=0;
	int r=s.size()-1;
	int cnt=0;
	vector<int> a(30,0);
	for(auto i:s)
	{
		a[i-'a']++;
	}
	for(int i=0;i<26;i++) cnt=max(cnt,a[i]);
	 cout<<s.size()-cnt<<'\n';
}
int main()
{
	IOS;
	int T=1;
	cin>>T;

	while(T--)
	{
		solve();
	}

	return 0;
}

